/**  
 * @Title: IPUtils.java
 * @Package org.wk.core.common.tool
 * @Description: TODO【】
 * @author df.w 
 * @date 2018年5月9日 上午11:31:50
 * @version V1.0  
 */ 

package org.wk.core.common.tool;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @ClassName	: IPUtils
 * @Package		：org.wk.core.common.tool
 * @Author df.w 
 * @Create Time	：2018年5月9日 上午11:31:50
 * @Description	: 
 */

public class IPUtils {
    /**
     * 获取IP地址
     *
     * 使用Nginx等反向代理软件， 则不能通过request.getRemoteAddr()获取IP地址
     * 如果使用了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP地址，X-Forwarded-For中第一个非unknown的有效IP字符串，则为真实IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;

        try {
            ip = request.getHeader("x-forwarded-for");
            if (ip==null||ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip==null||ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip==null||ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip==null||ip.length()==0|| "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip==null||ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            System.out.println("IPUtils ERROR "+e);
        }

//        //使用代理，则获取第一个IP地址
//        if(StringUtils.isEmpty(ip) && ip.length() > 15) {
//			if(ip.indexOf(",") > 0) {
//				ip = ip.substring(0, ip.indexOf(","));
//			}
//		}

        return ip;
    }
	
	
    public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getLocalHost();  
        String ip=addr.getHostAddress().toString(); //获取本机ip  
        String hostName=addr.getHostName().toString(); //获取本机计算机名称  
        System.out.println(ip);
        System.out.println(hostName);
        
        
        Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces();  
        while (interfs.hasMoreElements())  
        {  
            NetworkInterface interf = interfs.nextElement();  
            Enumeration<InetAddress> addres = interf.getInetAddresses();  
            while (addres.hasMoreElements())  
            {  
                InetAddress in = addres.nextElement();  
                if (in instanceof Inet4Address)  
                {  
                    System.out.println("v4:" + in.getHostAddress());  
                }  
                else if (in instanceof Inet6Address)  
                {  
                    System.out.println("v6:" + in.getHostAddress());  
                }  
            }  
        }
   }

}
